package com.nengchuan.kbms.dfs.controller.work;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nengchuan.excel.util.CsncExcelUtil;
import com.nengchuan.frame.auth.util.AuthUtils;
import com.nengchuan.kbms.dfs.entity.dto.*;
import com.nengchuan.kbms.dfs.entity.vo.UserOperateCreateVO;
import com.nengchuan.kbms.dfs.entity.vo.UserOperateDownloadVO;
import com.nengchuan.kbms.dfs.entity.vo.UserOperateHistoryVO;
import com.nengchuan.kbms.dfs.service.UserOperateHistoryService;
import com.nengchuan.model.model.NcPageInfo;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

// import AuthUtils;

/**
 * <p>
 * 用户操作历史 前端控制器
 * </p>
 *
 * @author abe
 * @since 2020-04-01
 */
@RestController
@RequestMapping("/work/operate")
@Api(tags = "知识库数据后台管理 > 工作管理 > 我的创建、我的导出、我的操作历史 【已完成】")
public class OperateController {

    @Autowired
    private UserOperateHistoryService userOperateHistoryService;

    @PostMapping("/create/page")
    @ApiOperation(value = "我的创建-列表")
    public WrappedResult<NcPageInfo<UserOperateCreateVO>> createPage(@RequestBody UserOperateCreatePageDTO pageDTO) {
        // pageDTO.setCreateId(AuthUtils.getUserId());
        IPage<UserOperateCreateVO> pageList = userOperateHistoryService.createPage(pageDTO);
        return WrappedResult.success(MapperUtils.mappingNcPage(pageList, UserOperateCreateVO.class));
    }

    @PostMapping(value = "/create/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation(value = "我的创建-导出列表")
    public void createExport(@RequestBody UserOperateCreateListDTO listDTO, HttpServletResponse response) throws Exception {
        List<UserOperateCreateVO> list = userOperateHistoryService.createList(listDTO);
        CsncExcelUtil.export(list, UserOperateCreateVO.class, response, "我的创建列表.xls");
    }

    @PostMapping("/download/page")
    @ApiOperation(value = "我的导出-列表")
    public WrappedResult<NcPageInfo<UserOperateDownloadVO>> downloadPage(@RequestBody UserOperateDownloadPageDTO pageDTO) {
        pageDTO.setCreateId(AuthUtils.getUserId());
        IPage<UserOperateDownloadVO> pageList = userOperateHistoryService.downloadPage(pageDTO);
        return WrappedResult.success(MapperUtils.mappingNcPage(pageList, UserOperateDownloadVO.class));
    }

    @PostMapping(value = "/download/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation(value = "我的导出-导出列表")
    public void downloadExport(@RequestBody UserOperateDownloadListDTO listDTO, HttpServletResponse response) throws Exception {
        List<UserOperateDownloadVO> list = userOperateHistoryService.downloadList(listDTO);
        CsncExcelUtil.export(list, UserOperateDownloadVO.class, response, "我的导出列表.xls");
    }

    @PostMapping("/history/page")
    @ApiOperation(value = "我的操作历史-列表")
    public WrappedResult<NcPageInfo<UserOperateHistoryVO>> historyPage(@RequestBody UserOperateHistoryPageDTO pageDTO) {
        pageDTO.setCreateId(AuthUtils.getUserId());
        return WrappedResult.success(userOperateHistoryService.page(pageDTO, UserOperateHistoryVO.class));
    }

    @PostMapping(value = "/history/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation(value = "我的操作历史-导出列表")
    public void export(@RequestBody UserOperateHistoryPageDTO pageDTO, HttpServletResponse response) throws Exception {
        List<UserOperateHistoryVO> list = userOperateHistoryService.list(pageDTO, UserOperateHistoryVO.class);
        CsncExcelUtil.export(list, UserOperateHistoryVO.class, response, "我的操作历史列表.xls");
    }

}
